package com.supreme.studyserver.service;

import com.supreme.studyserver.springbase.SpringComponent;
import com.supreme.studyserver.springbase.SpringDAO;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Service;

/**
 * Spring基础功能学习
 */
@Service
public class SpringService {

    /**
     * 使用ClassPathXmlApplicationContext
     */
    public void testSpringDaoType() {
        final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:context/application.xml");
        final SpringDAO springDAO = applicationContext.getBean(SpringDAO.class);
        System.out.println(springDAO);
    }

    public void testSpringDaoName() {
        final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:context/application.xml");
        final SpringDAO springDAO = (SpringDAO) applicationContext.getBean("springDAO");
        System.out.println(springDAO);
    }

    public void testSpringDaoNameType() {
        final String userDirProperty = System.getProperty("user.dir");
        System.out.println(userDirProperty);
        final ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:context/application.xml");
        final SpringDAO springDAO = applicationContext.getBean("springDAO", SpringDAO.class);
        System.out.println(springDAO);
    }

    public void testSpringDaoExist(String beanName) {
        final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("context/application.xml");
        final boolean containsBean = applicationContext.containsBean(beanName);
        System.out.println(containsBean);
    }

    public void testSpringComponent() {
        final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:context/application.xml");
        final SpringComponent springComponent = applicationContext.getBean(SpringComponent.class);
        springComponent.testMethod();
    }
}
